package controller

import (
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
	log "github.com/sirupsen/logrus"
	"time"
)

type database struct {
	*gorm.DB
}

var (
	DB *gorm.DB
	sqlConnection = "root:root@(127.0.0.1:3306)/ginweb?charset=utf8&parseTime=True&loc=Local"
)

func Init() *gorm.DB  {
	db, err := gorm.Open("mysql", sqlConnection)
	if err != nil {
		ConfigLocalFilesystemLogger("./runtime/logs/", "database.log", 7*24*time.Hour, 24*time.Hour)
		log.Error("mysql连接失败，原因：", err)
	}
	defer db.Close()
	db.DB().SetMaxIdleConns(10)
	DB = db
	return DB
}

func GetDB() *gorm.DB  {
	return DB
}